package com.wn.intercepter;

import com.wn.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yellow Docter
 * @date 2022 -06-10
 * @desc
 *  jwt 的拦截器
 */
@Slf4j
public class JwtInterceptor implements HandlerInterceptor {

    /**
     * 拦截器 拦截的方法
     *
     * 01  如果 结果为true     放行 会执行 controller 的对应方法
     * 01  如果 结果为false    不放行  不会执行 controller
     *
     *
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 每次请求 会携带token
        //通过 request 来获取
        String token = JwtTokenUtils.getJwtToken(request);
        if (token == null) {
            throw new Exception("判断toten 是否存在...");
        }

        //isverfiy=false代表过期
        boolean isverfiy = JwtTokenUtils.isverfiy(token);
        if(!isverfiy){
            throw new Exception("判断是否过期...");
        }
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
